
//
//  SearchController.m
//  WANNA
//
//  Created by 请叫我灬阿文 on 16/5/7.
//  Copyright © 2016年 浮夸. All rights reserved.
//

#import "SearchController.h"
#import "drawerViewController.h"
@interface SearchController ()<UISearchBarDelegate,UISearchResultsUpdating>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *searchArray;
@property (nonatomic,strong)UISearchController *search;
@end

@implementation SearchController

- (void)viewDidLoad {
    self.title=@"搜索";
    [super viewDidLoad];
    self.dataArray = [NSMutableArray arrayWithCapacity:100];
    for (int i =0; i <100; i ++) {
        [self.dataArray addObject:[NSString stringWithFormat:@"%d主题曲",i]];
    }
    self.searchArray = [NSMutableArray new];
    
    _search = [[UISearchController alloc] initWithSearchResultsController:nil];
    _search.searchResultsUpdater = self;
    _search.dimsBackgroundDuringPresentation = NO;
    _search.hidesNavigationBarDuringPresentation = NO;
    self.search.searchBar.frame = CGRectMake(0, 0, ScreenWidth, 44);
    self.tableView.tableHeaderView =self.search.searchBar;
    
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithTitle:@"💢" style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = backBtn;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    
}
-(void)backAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.search.active) {
        return self.searchArray.count;
    }else
    {
        return self.dataArray.count;
    }
    }
#warning Incomplete implementation, return the number of rows
    


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *resuID =@"cell_id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:resuID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:resuID];
        
    }
                             
    if (self.search.active) {
        cell.textLabel.text =self.searchArray[indexPath.row];
    }else{
        cell.textLabel.text =self.dataArray[indexPath.row];
    }
    // Configure the cell...
    
    return cell;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = [self.search.searchBar text];
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchArray!= nil) {
        [self.searchArray removeAllObjects];
    }
    //过滤数据
    self.searchArray= [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索Begin");
    return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"搜索End");
    return YES;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
